# coding: utf-8
from PySide6.QtCore import Qt
from PySide6.QtWidgets import QWidget, QListWidgetItem, QListWidget
from qfluentwidgets import SmoothScrollDelegate


class CustomListWidget(QListWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.widgets = []
        self.scrollDelegate = SmoothScrollDelegate(self)

        self.setSpacing(10)

        self.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
        self.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)

    def addWidget(self, widget: QWidget):
        item = QListWidgetItem()
        item.setSizeHint(widget.sizeHint())
        self.widgets.append(widget)
        self.addItem(item)
        self.setItemWidget(item, widget)

    def removeWidget(self, widget: QWidget):
        for i in range(self.count()):
            item = self.item(i)
            if self.itemWidget(item) == widget:
                self.takeItem(i)
                widget.deleteLater()
                break

    def takeAll(self):
        while self.count() > 0:
            item: QListWidgetItem = self.takeItem(0)
            widget = self.itemWidget(item)
            if widget:
                widget.deleteLater()
            del item
